<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>
<%@page import="support.*, java.util.*" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style>
<!-- Three column style settings -->
#left {
  margin-right: 153px;
}
#right {
  margin-left: 150px;
}

#left_wrapper {
  width: 50%;
  margin-right: -150px;
  float: left;
  position: relative;
  z-index: 0;
}

#middle_wrapper {
  width: 300px;
  margin-right: -148px;
  float: left;
  position: relative;
  z-index: 2;
}

#right_wrapper {
  width: 49.9%;
  float: left;
  position: relative;
  z-index: 1;
}
</style>
<title>Degree - Choose University</title>

<script type="text/javascript">
function univExists(location){
	var xmlHttp;
	  xmlHttp=new XMLHttpRequest();

	  var responseHandler = function()
	   {
	    if(xmlHttp.readyState==4)
	     { document.getElementById("error").innerHTML = xmlHttp.responseText;
	       var response = xmlHttp.responseText;
	       response = response.replace(/(\s)*/,"");
	       if(response){
	    	   document.forms["other"]["submit"].disabled=true;
	       }else{
	           document.forms["other"]["submit"].disabled=false;
	       }
	     }
	   };

	  xmlHttp.onreadystatechange = responseHandler;

	  xmlHttp.open("GET","checkUniversity.jsp?university=" +document.forms["other"]["university"].value + "&location=" + location ,true);
	  xmlHttp.send(null);
  
}
</script>

</head>
<body>
    <!--  Book keeping for university.jsp -->
    <%
    
      // add the location to our current degree set
      String degreeLocation = request.getParameter("degreeLocation");
      LinkedHashSet<String> currentDegree = (LinkedHashSet<String>)session.getAttribute("currentDegree");
      currentDegree.add(degreeLocation);
      ArrayList<String> universities = new ArrayList<String>();
    %>
    
    <!-- Hidden variable for university -->
    <input type="hidden" name="university" value="">
   
    Select University in <%= degreeLocation  %>: <br> <br>

    Other University:
	<!-- TEXT BOX FOR INSERT YOURSELF -->
	<form method="GET" action="discipline.jsp" name="other">
       <font color="red"><span id="error" ></span></font><br>
       <% out.print("<input type=\"text\" size=20 name=\"university\" onkeyup=\"univExists('" + degreeLocation +  "');\""); %>
	   <input id="submit" type=submit name="Submit">
	</form>
	
    <!-- THREE COLUMNS OF UNIVERSITIES -->
    <%
    // Connect to ConnectionPool
    Connection conn = null;
    try{
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/CS135");
        conn = ds.getConnection();
    }finally{
    }
    Statement stmt = conn.createStatement();
    
    ResultSet rset = stmt.executeQuery("SELECT uname FROM  universities WHERE loc = \'" + degreeLocation + "\'");
    // populate the ArrayList
    while(rset.next()){
        universities.add(rset.getString("uname"));
    }
    
    request.getSession(false).setAttribute("universities", universities);
    
    rset = stmt.executeQuery("SELECT COUNT(*) FROM  universities WHERE loc = \'" + degreeLocation + "\'");
    int counter = 0;  // keeps track of the university we are on
    int universitySize = 0;  // keeps track of the size of the table
    if(rset.next()){
        universitySize = rset.getInt(1);
    }
    %>
    
    <%
    // use sql statement to display all the universities
    rset = stmt.executeQuery("SELECT * FROM  universities WHERE loc = \'" + degreeLocation + "\'");
   
    %> 
    
    <!-- Create the LEFT COLUMN -->
    <div id="left_wrapper">
    <div id="left">
    <%
     for(int i=0; i<universitySize/3; i++, counter++){
       // use hidden object to make href
       if(rset.next()){
       out.print(
               "<a href=\"discipline.jsp?university=" + rset.getString(2)
               + "\">" + rset.getString(2) + "</a>" + "<br>");   
       }
     }
    %>
    </div>
    </div>
    
     <!-- Create the MIDDLE COLUMN -->
    <div id="middle_wrapper">
    <div id="middle">
    <%
     for(int i =0;i<universitySize/3; i++, counter++){
         // use hidden object to make href
         if(rset.next()){
         out.print(
                 "<a href=\"discipline.jsp?university=" + rset.getString(2)
                 + "\">" + rset.getString(2) + "</a>" + "<br>");      
         }
     }
    %>
    </div>
    </div>
    
     <!-- Create the RIGHT COLUMN -->
    <div id="right_wrapper">
    <div id="right">
    <%
     for(; counter<universitySize; counter++){
         // use hidden object to make href
         if(rset.next()){
         out.print(
                 "<a href=\"discipline.jsp?university=" + rset.getString(2)
                 + "\">" + rset.getString(2) + "</a>" + "<br>"); 
         }
      }
    %>
    </div>
    </div>
</body>
</html>